#coding:utf-8
import re
import requests
import time
import os
import sys
from bs4 import BeautifulSoup

#referer 盗链
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0','Referer':'http://www.mzitu.com/'}
index = 'http://www.mzitu.com/all/'
r = requests.get(index,headers=headers)
soup = BeautifulSoup(r.content,'lxml')
a_url = soup.find('div',class_='all').findAll('a')
for a in a_url:
	# time.sleep(2)
	title = a.get_text()#.encode('utf-8')
	print title
	href = a['href']
	#path = href.split('/')[3]#创建文件路径
	#print path
	os.mkdir(unicode(title))#以标题创建文件夹
	html = requests.get(href,headers=headers)
	html_soup = BeautifulSoup(html.content,'lxml')
	#分页栏目，倒数第二个标签是最大标签页
	max_page = html_soup.find('div',class_='pagenavi').findAll('span')[-2].get_text()#得到最大页数
	# print type(max_page)
	# print title +'---->'+ href
	max_page = int(max_page)+1
	for page in range(1,max_page):
		page_url = href + '/' + str(page)
		#img地址
		image_html = requests.get(page_url,headers=headers)
		image_soup = BeautifulSoup(image_html.content,'lxml')
		image_url = image_soup.find('div',class_='main-image').find('img')['src']
		#图片名字
		name = image_url[-9:-4]
		img = requests.get(image_url,headers=headers)
		f = open(unicode(title)+'/'+name+'.jpg','wb+')
		f.write(img.content)
		print name + '----> write success'
		f.close()
		continue
